﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using AvalonDock;
using Foundation;
using Foundation.MarkupExtensions;
using Poet.Modules;
using Poet.Properties;
using Poet.Views;
using Application = System.Windows.Application;
using MenuItem = System.Windows.Controls.MenuItem;

namespace Poet.Resources
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class XamlStore
    {
        public XamlStore()
        {
            InitializeComponent();
        }

        //private void ChangeFont(object sender, RoutedEventArgs e)
        //{
        //    var trunkView = (TrunkView)Application.Current.MainWindow;
        //    //var view = trunkView.FontView;
        //    //view.Show(trunkView.DockingManager);
        //    //view.Activate();
        //}

        //private void ChangeBrush(object sender, RoutedEventArgs e)
        //{
        //    var trunkView = (TrunkView)Application.Current.MainWindow;
        //    //var view = trunkView.PaletteView;
        //    //view.Show(trunkView.DockingManager);
        //    //view.Activate();
        //}

        private void IgnoreGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            e.Handled = true;
        }

        private void SetDefaultTheme(object sender, RoutedEventArgs e)
        {
            ThemeManager.SetDefaults();
        }

        private void ChangeTheme(object sender, RoutedEventArgs e)
        {
            var name = (string)((MenuItem)sender).Tag;
            ThemeManager.SetAvalonDockTheme(name);
        }

        private void ChangeColor(object sender, RoutedEventArgs e)
        {
            var colorName = ((MenuItem)sender).Header.ToString();
            ThemeManager.SetAvalonDockColor(colorName);
        }

        private void MenuItemClick(object sender, RoutedEventArgs e)
        {
            var menuItem = (MenuItem)sender;
            var tag = (string)menuItem.Tag;
            MenuItemClick(tag);
        }

        private void MenuItemClick(string tag)
        {
            switch (tag)
            {
                case "Ru":
                    Localizing.Managers.Clear();
                    Localizing.Managers.Add(Foundation.Properties.Resources.ResourceManager);
                    Localizing.Managers.Add(RussianResource.ResourceManager);
                    Localizing.RaiseLocalizingChanged();
                    Settings.Default.Language = "Ru";
                    Settings.Default.Save();
                    break;

                case "En":
                    Localizing.Managers.Clear();
                    Localizing.Managers.Add(Foundation.Properties.Resources.ResourceManager);
                    Localizing.RaiseLocalizingChanged();
                    Settings.Default.Language = "En";
                    Settings.Default.Save();
                    break;

                case "ChangeAeroThemeColor":
                    var d = new ColorDialog();
                    if (d.ShowDialog() == DialogResult.OK)
                        ThemeFactory.ChangeColors(Color.FromRgb(d.Color.R, d.Color.G, d.Color.B));
                    break;

                case "ShowNavigatorWindow":
                    ((TrunkView)(Application.Current.MainWindow)).DockingManager.ShowNavigatorWindow();
                    break;
            }
        }

        private void OnCheckDocumentView(object sender, RoutedEventArgs e)
        {
            var menuItem = (MenuItem)e.OriginalSource;
            var newCheckedDocument = (ManagedContent)menuItem.DataContext;
            newCheckedDocument.Show(Store.OfType<TrunkView>().DockingManager);
            newCheckedDocument.Activate();
        }

        private void OnCheckDocumentView1(object sender, RoutedEventArgs e)
        {
            var menuItem = (MenuItem)e.OriginalSource;
            var newCheckedDocument = (DockableContent)menuItem.DataContext;
            newCheckedDocument.ShowAsDocument(Store.OfType<TrunkView>().DockingManager);
            newCheckedDocument.Activate();
        }

        private void ToggleButton_OnClick(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            var toggleButton = (Expander) sender;
            var stackPanel = Foundation.ExtensionMethods.VisualTreeExtensions.GetNearestVisualAncestor<StackPanel>(toggleButton);
            var contextMenu = stackPanel.ContextMenu;
            if (contextMenu == null) return;
            contextMenu.PlacementTarget = stackPanel;
            contextMenu.IsOpen = true;
        }

        private void Clear_OnLoaded(object sender, RoutedEventArgs e)
        {
            var menuItem = (MenuItem) sender;
            menuItem.CommandTarget = Application.Current.MainWindow;
        }

        private void UIElement_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            var control = (Expander)sender;
            control.Focus();
            var contextMenu = control.ContextMenu;
            if (contextMenu == null) return;
            contextMenu.PlacementTarget = control;
            contextMenu.IsOpen = true;
        }
    }
}
